﻿#Region "License & Usage"

'Microsoft Public License (Ms-PL) 

'This license governs use of the accompanying software. If you use the 
'software, you accept this license. If you do not accept the license, do 
'not use the software. 

'1. Definitions 

'The terms "reproduce," "reproduction," "derivative works," and 
'"distribution" have the same meaning here as under U.S. copyright law. 

'A "contribution" is the original software, or any additions or changes 
'to the software. 

'A "contributor" is any person that distributes its contribution under 
'this license. 

'"Licensed patents" are a contributor's patent claims that read directly 
'on its contribution. 

'2. Grant of Rights 

'(A) Copyright Grant- Subject to the terms of this license, including the 
'license conditions and limitations in section 3, each contributor grants 
'you a non-exclusive, worldwide, royalty-free copyright license to 
'reproduce its contribution, prepare derivative works of its 
'contribution, and distribute its contribution or any derivative works 
'that you create. 

'(B) Patent Grant- Subject to the terms of this license, including the 
'license conditions and limitations in section 3, each contributor grants 
'you a non-exclusive, worldwide, royalty-free license under its licensed 
'patents to make, have made, use, sell, offer for sale, import, and/or 
'otherwise dispose of its contribution in the software or derivative 
'works of the contribution in the software. 

'3. Conditions and Limitations 

'(A) No Trademark License- This license does not grant you rights to use 
'any contributors' name, logo, or trademarks. 

'(B) If you bring a patent claim against any contributor over patents 
'that you claim are infringed by the software, your patent license from 
'such contributor to the software ends automatically. 

'(C) If you distribute any portion of the software, you must retain all 
'copyright, patent, trademark, and attribution notices that are present 
'in the software. 

'(D) If you distribute any portion of the software in source code form, 
'you may do so only under this license by including a complete copy of 
'this license with your distribution. If you distribute any portion of 
'the software in compiled or object code form, you may only do so under a 
'license that complies with this license. 

'(E) The software is licensed "as-is." You bear the risk of using it. The 
'contributors give no express warranties, guarantees or conditions. You 
'may have additional consumer rights under your local laws which this 
'license cannot change. To the extent permitted under your local laws, 
'the contributors exclude the implied warranties of merchantability, 
'fitness for a particular purpose and non-infringement.

#End Region

#Region "Options"

Option Strict On
Option Explicit On

#End Region

#Region "Imports"

'From .Net Framework 3.5
Imports Microsoft.Drawing
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms

'From Custom Classes/Reference
Imports StedySoft.SenseSDK
Imports StedySoft.SenseSDK.DrawingCE
Imports SenseSDKExtended.DrawingCEEx

#End Region

Namespace SensePanelSelectionControl

    ''' <summary>SenseSelectionControl.SelectionStyle1</summary>
    ''' <author>Greenhacks</author>
    ''' <author-reference>None</author-reference>
    ''' <copyright>None</copyright>
    ''' <version>0.1</version>
    ''' <remarks>None</remarks>
    ''' <reference>None</reference>
    Public Class SelectionStyle1
        Inherits SensePanelBase
        Implements IDisposable

#Region "Fields"

        'General Flags
        Private _disposed As Boolean = False
        Private _buttonSelected As Boolean = False

        'General Properties
        Private _name As String = Nothing

        Private _imageEnabled As IImage = _getIImageFromResource("selection.gray")
        Private _imagePressed As IImage = _getIImageFromResource("selection.green")

        Private _days As ArrayList = New ArrayList

        Private _firstDayOfTheWeek As Microsoft.VisualBasic.FirstDayOfWeek

        Private _sundaySelected As Boolean
        Private _mondaySelected As Boolean
        Private _tuesdaySelected As Boolean
        Private _wednesdaySelected As Boolean
        Private _thursdaySelected As Boolean
        Private _fridaySelected As Boolean
        Private _saturdaySelected As Boolean

#End Region

#Region "Methods"

        ''' <summary>
        ''' Constructor
        ''' </summary>
        ''' <remarks>None</remarks>
        Public Sub New()

            Try

                'Do Nothing

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Sub

        ''' <summary>
        ''' Constructor
        ''' </summary>
        ''' <remarks>None</remarks>
        Public Sub New(ByVal Name As String)

            Try

                'Save name property
                _name = Name

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Sub

        ''' <summary>
        ''' Dispose
        ''' </summary>
        ''' <remarks>None</remarks>
        Public Overrides Sub Dispose()

            Try

                Me._dispose(True)
                GC.SuppressFinalize(Me)
                MyBase.Dispose()

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Sub

        ''' <summary>
        ''' _Dispose
        ''' </summary>
        ''' <param name="disposing"></param>
        ''' <remarks>None</remarks>
        Private Sub _dispose(ByVal disposing As Boolean)

            Try

                Me._disposed = True

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try
        End Sub

        ''' <summary>
        ''' Check if it is VGA or QVGA
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function _isVGA() As Boolean

            Try

                'Return the output
                Return StedySoft.SenseSDK.DrawingCE.Resolution.ScreenIsVGA

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Function

        ''' <summary>
        ''' _getIImageFromResource
        ''' </summary>
        ''' <param name="resource"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function _getIImageFromResource(ByVal resource As String) As IImage
            Dim iimg As IImage = Nothing
            Using strm As MemoryStream = If(_isVGA, SenseSDKExtended.Resources.VGA.Images.GetIImageFromResource(resource), SenseSDKExtended.Resources.QVGA.Images.GetIImageFromResource(resource))
                ImagingFactory.GetImaging.CreateImageFromBuffer(strm.GetBuffer, Convert.ToUInt32(strm.Length), BufferDisposalFlag.BufferDisposalFlagNone, iimg)
            End Using
            Return iimg
        End Function

        <CLSCompliant(False)> _
        Public Structure DayStructure

            Dim _dayButtonRectangle As Rectangle
            Dim _dayButtonAbbreviate As String
            Dim _dayButtonSelected As Boolean

        End Structure

        Public Event OnClickDays(ByVal sender As Object, ByVal value As String)

        Public Overrides Sub OnMouseDown(ByVal x As Integer, ByVal y As Integer, ByRef cancelScroll As Boolean)
            MyBase.OnMouseDown(x, y, cancelScroll)

        End Sub

        Public Overrides Sub OnMouseUp(ByVal x As Integer, ByVal y As Integer)
            MyBase.OnMouseUp(x, y)

            Dim mousePoint As System.Drawing.Point = System.Windows.Forms.Control.MousePosition()
            Dim pointX As Integer = mousePoint.X - MyBase.ClientRectangle.X
            Dim pointY As Integer = (mousePoint.Y - (Screen.PrimaryScreen.Bounds.Height - DrawingCE.Resolution.ScreenHeight)) - ClientRectangle.Top
            Dim _selectedDays As String = ","

            For i As Integer = 0 To 6

                Select Case pointX

                    Case DirectCast(DirectCast(_days.Item(i), System.Object), SelectionStyle1.DayStructure)._dayButtonRectangle.Left To DirectCast(DirectCast(_days.Item(i), System.Object), SelectionStyle1.DayStructure)._dayButtonRectangle.Left + DirectCast(DirectCast(_days.Item(i), System.Object), SelectionStyle1.DayStructure)._dayButtonRectangle.Width

                        Select Case pointY

                            Case DirectCast(DirectCast(_days.Item(i), System.Object), SelectionStyle1.DayStructure)._dayButtonRectangle.Top - ClientRectangle.Top To (DirectCast(DirectCast(_days.Item(i), System.Object), SelectionStyle1.DayStructure)._dayButtonRectangle.Top - ClientRectangle.Top) + DirectCast(DirectCast(_days.Item(i), System.Object), SelectionStyle1.DayStructure)._dayButtonRectangle.Height

                                Select Case DirectCast(DirectCast(_days.Item(i), System.Object), SelectionStyle1.DayStructure)._dayButtonAbbreviate

                                    Case "Sun"
                                        If _sundaySelected Then _sundaySelected = False Else _sundaySelected = True

                                    Case "Mon"
                                        If _mondaySelected Then _mondaySelected = False Else _mondaySelected = True

                                    Case "Tue"
                                        If _tuesdaySelected Then _tuesdaySelected = False Else _tuesdaySelected = True

                                    Case "Wed"
                                        If _wednesdaySelected Then _wednesdaySelected = False Else _wednesdaySelected = True

                                    Case "Thu"
                                        If _thursdaySelected Then _thursdaySelected = False Else _thursdaySelected = True

                                    Case "Fri"
                                        If _fridaySelected Then _fridaySelected = False Else _fridaySelected = True

                                    Case "Sat"
                                        If _saturdaySelected Then _saturdaySelected = False Else _saturdaySelected = True

                                End Select

                        End Select

                End Select

            Next

            If _sundaySelected Then _selectedDays &= "Sun,"
            If _mondaySelected Then _selectedDays &= "Mon,"
            If _tuesdaySelected Then _selectedDays &= "Tue,"
            If _wednesdaySelected Then _selectedDays &= "Wed,"
            If _thursdaySelected Then _selectedDays &= "Thu,"
            If _fridaySelected Then _selectedDays &= "Fri,"
            If _saturdaySelected Then _selectedDays &= "Sat,"

            _selectedDays = _selectedDays.TrimStart(CChar(",")).TrimEnd(CChar(","))

            RaiseEvent OnClickDays(Me, _selectedDays)

        End Sub

        ''' <summary>
        ''' OnRender
        ''' </summary>
        ''' <param name="g"></param>
        ''' <remarks></remarks>
        Public Overrides Sub OnRender(ByVal g As System.Drawing.Graphics)
            MyBase.OnRender(g)

            Dim _leftStart As Integer = CInt((Screen.PrimaryScreen.Bounds.Width - If(_isVGA, 350, 175)) / 2)
            Dim _buttonWidth As Integer = If(_isVGA, 50, 25)
            Dim _buttonCount As Integer = 7
            Dim _dayAbbreviate As String = Nothing
            Dim _dayAbbreviateSingle As String = Nothing
            Dim _imageButton As IImage = Nothing
            Dim _dayDetails As DayStructure = New DayStructure

            _days.Clear()

            For Count As Integer = 0 To _buttonCount - 1

                _dayAbbreviate = WeekdayName(Count + 1, True, _firstDayOfTheWeek)

                Select Case _dayAbbreviate

                    Case "Sun"
                        _dayDetails._dayButtonSelected = Sunday
                    Case "Mon"
                        _dayDetails._dayButtonSelected = Monday
                    Case "Tue"
                        _dayDetails._dayButtonSelected = Tuesday
                    Case "Wed"
                        _dayDetails._dayButtonSelected = Wednesday
                    Case "Thu"
                        _dayDetails._dayButtonSelected = Thursday
                    Case "Fri"
                        _dayDetails._dayButtonSelected = Friday
                    Case "Sat"
                        _dayDetails._dayButtonSelected = Saturday

                End Select

                _dayAbbreviateSingle = _dayAbbreviate.Substring(0, 1)

                _imageButton = If(_dayDetails._dayButtonSelected, _imagePressed, _imageEnabled)

                g.DrawImageAlphaChannel(_imageButton, New Rectangle(_leftStart + (_buttonWidth * Count), ClientRectangle.Top + CInt((ClientRectangle.Height - If(_isVGA, 50, 25)) / 2), If(_isVGA, 50, 25), If(_isVGA, 50, 25)))

                Using f As Font = New Font("Helvetica Neue OTS", 9.0F, FontStyle.Regular)
                    Dim s As String = _dayAbbreviateSingle
                    Dim r As Rectangle = New Rectangle(_leftStart + (_buttonWidth * Count), ClientRectangle.Top + CInt((ClientRectangle.Height - If(_isVGA, 50, 25)) / 2), If(_isVGA, 50, 25), If(_isVGA, 50, 25))
                    Using b As Brush = New SolidBrush(If(_dayDetails._dayButtonSelected, Color.White, Color.Gray))
                        Using sf As StringFormat = New StringFormat()
                            sf.Alignment = StringAlignment.Center
                            sf.LineAlignment = StringAlignment.Center
                            g.DrawString(s, f, b, r, sf)
                        End Using
                    End Using
                End Using


                _dayDetails._dayButtonAbbreviate = _dayAbbreviate
                _dayDetails._dayButtonRectangle = New Rectangle(_leftStart + (_buttonWidth * Count), ClientRectangle.Top + CInt((ClientRectangle.Height - If(_isVGA, 50, 25)) / 2), If(_isVGA, 50, 25), If(_isVGA, 50, 25))

                _days.Add(_dayDetails)

            Next

        End Sub

#End Region

#Region "Properties"

        Public Property FirstDayOfTheWeek() As Microsoft.VisualBasic.FirstDayOfWeek
            Get
                Return _firstDayOfTheWeek
            End Get
            Set(ByVal value As Microsoft.VisualBasic.FirstDayOfWeek)
                _firstDayOfTheWeek = value
            End Set
        End Property

        Public Property Sunday() As Boolean
            Get
                Return _sundaySelected
            End Get
            Set(ByVal value As Boolean)
                _sundaySelected = value
            End Set
        End Property

        Public Property Monday() As Boolean
            Get
                Return _mondaySelected
            End Get
            Set(ByVal value As Boolean)
                _mondaySelected = value
            End Set
        End Property

        Public Property Tuesday() As Boolean
            Get
                Return _tuesdaySelected
            End Get
            Set(ByVal value As Boolean)
                _tuesdaySelected = value
            End Set
        End Property

        Public Property Wednesday() As Boolean
            Get
                Return _wednesdaySelected
            End Get
            Set(ByVal value As Boolean)
                _wednesdaySelected = value
            End Set
        End Property

        Public Property Thursday() As Boolean
            Get
                Return _thursdaySelected
            End Get
            Set(ByVal value As Boolean)
                _thursdaySelected = value
            End Set
        End Property

        Public Property Friday() As Boolean
            Get
                Return _fridaySelected
            End Get
            Set(ByVal value As Boolean)
                _fridaySelected = value
            End Set
        End Property

        Public Property Saturday() As Boolean
            Get
                Return _saturdaySelected
            End Get
            Set(ByVal value As Boolean)
                _saturdaySelected = value
            End Set
        End Property

#End Region

    End Class

End Namespace